package com.minisoft.utils;

import cn.hutool.core.util.StrUtil;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.PathMatcher;

import java.util.HashMap;
import java.util.Map;

/**
 * @author JueSu
 * @description: 转发拦截器的配置类
 * @date 2021/9/4 14:45
 */
public class DispatchConfig {

    private Map<String,ConfigBean> configMap = new HashMap<>();

    private PathMatcher pathMatcher = new AntPathMatcher();
    /**
     * 增加映射配置
     * @param prefixPattern 请求路径的前缀，比如目标服务的路径是/customer/call,prefixPattern配置成/pool
     *                           那么前端访问和拦截的就是：/pool/customer/call
     * @param patterns 目标服务的路径
     * @param serviceUrl 目标服务地址
     * @param appid 网关的appid
     * @param secret 网关的密钥
     */
    public void addMappingConfig(String prefixPattern,String patterns,String serviceUrl,
                                 String appid,String secret) {
        String[] patternArray = patterns.split(",");
        for (String pattern : patternArray) {
            ConfigBean configBean = new ConfigBean(pattern, prefixPattern,serviceUrl, appid, secret);
            configMap.put(configBean.getUrlPattern(),configBean);
        }

    }
    /**
     * 增加映射配置
     * @param patterns 目标服务的路径
     * @param serviceUrl 目标服务地址
     * @param appid 网关的appid
     * @param secret 网关的密钥
     */
    public void addMappingConfig(String patterns,String serviceUrl,
                                 String appid,String secret) {
        addMappingConfig(null,patterns,serviceUrl, appid, secret);
    }

    protected String getUrl(String uri) {
        ConfigBean configBean = getConfigBean(uri);
        if (StrUtil.isNotBlank(configBean.prefixPattern)) {
            //如果有前缀，需要先去掉，然后再拼接转发地址
            uri = uri.substring(configBean.prefixPattern.length());
        }
        return configBean.serviceUrl + uri;
    }

    /**
     * 根据uri找到配置对象
     * @param uri
     * @return
     */
    private ConfigBean getConfigBean(String uri) {
        for (String pattern : configMap.keySet()) {
            if (pathMatcher.match(pattern,uri)) {
                return configMap.get(pattern);
            }
        }
        return null;
    }

    public String getSecret(String uri) {
        ConfigBean configBean = getConfigBean(uri);
        return configBean.secret;
    }

    public String getAppid(String uri) {
        ConfigBean configBean = getConfigBean(uri);
        return configBean.appid;
    }

    public String[] getPathPatterns() {
        return configMap.keySet().toArray(new String[0]);
    }

    class ConfigBean {
        String pattern;
        String prefixPattern;
        String serviceUrl;
        String appid;
        String secret;
        public ConfigBean(String pattern, String prefixPattern,
                          String serviceUrl, String appid, String secret) {
            this.pattern = pattern;
            this.prefixPattern = prefixPattern;
            this.serviceUrl = serviceUrl;
            this.appid = appid;
            this.secret = secret;
        }

        public String getUrlPattern() {
            if (StrUtil.isBlank(prefixPattern)) {
                return pattern;
            }
            return prefixPattern + pattern;
        }
    }
}
